package com.lanxiao.cloud.common.web.aop;

import com.lanxiao.cloud.common.core.service.IOperatorService;
import java.lang.reflect.Method;
import java.util.Objects;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * 重复提交切面
 *
 * @author ：qiaoliang
 * @date ：2021-08-19
 */
@Aspect
@Component
public class ResubmitAspect {

  /**重复提交验证缓存规则key**/
  public static final String INTEGRAL_RESUBMIT_USER_KEY = "resubmit:user:";

  private final IOperatorService operatorService;

  public ResubmitAspect(IOperatorService operatorService) {
    this.operatorService = operatorService;
  }


  @Pointcut("@annotation(com.lanxiao.cloud.common.web.invalid.ResubmitAnnotation)")
  public void resubmit(){}

  /**
   * 方法执行前
   */
  @Before(value = "resubmit()")
  public void before(JoinPoint joinPoint){
    Long currentUserId = operatorService.operator();
    if(Objects.nonNull(currentUserId)){
      Method method = getMethod(joinPoint);
//      ResubmitAnnotation annotation = method.getAnnotation(ResubmitAnnotation.class);
//      String key = key(annotation.businessType(),method.getName(),currentUserId);
//      Object createByRedis = RedisTemplateUtils.get(key);
//      if(Objects.nonNull(createByRedis)){
//        throw new BusinessException(annotation.errorMessage());
//      }else {
//        RedisTemplateUtils.set(key,annotation.resubmitTime(),currentUserId);
//      }
    }
  }

  /**
   * 方法执行结束，不管是抛出异常或者正常退出都会执行
   */
  @After(value = "resubmit()")
  public void after(JoinPoint joinPoint){
    Long currentUserId = operatorService.operator();
    if(Objects.nonNull(currentUserId)){
      Method method = getMethod(joinPoint);
//      ResubmitAnnotation annotation = method.getAnnotation(ResubmitAnnotation.class);
//      RedisTemplateUtils.delete(key(annotation.businessType(),method.getName(),currentUserId));
    }
  }

  private String key(String businessType ,String methodName , String currentUserId){
    return INTEGRAL_RESUBMIT_USER_KEY + businessType +
        ":" + methodName + ":" + currentUserId;
  }

  private Method getMethod(JoinPoint joinPoint){
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    //得到目标方法
    return signature.getMethod();
  }

}
